//todo to filter the user's permission routes when they log in

import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";

import Layout from "../layout/index.vue";

import {getSession} from "@/utils/session";

import store from "../store";
import fr from "element-ui/src/locale/lang/fr"

Vue.use(VueRouter);
const whiteList = ['Login']
const routes = [
  {
    path: "/",
    component: Layout,
    redirect: "/login",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        meta: {
          title: "首页",
          isKeepAlive: true
        },
        component: () => import("@/views/dashboard/index.vue")
      }
    ]
  },
  {
    path: '/',
    redirect:'/login'
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue")
  },
  {
    path: "/404",
    component: Layout,
    children: [
      {
        path: "index",
        name: "Error404",
        meta: {
          title: "404"
        },
        component: () => import("@/views/error/error404.vue")
      }
    ]
  },
  {
    path: "/news",
    component: Layout,
    children: [
      {
        path: "index",
        name: "News",
        meta: {
          title: "新闻公告"
        },
        component: () => import("@/views/news/index.vue")
      }
    ]
  },
  {
    path: "/online",
    component: Layout,
    children: [
      {
        path: "index",
        name: "Online",
        meta: {
          title: "在线人数"
        },
        component: () => import("@/views/online/index.vue")
      }
    ]
  },
  {
    path: "/user",
    component: Layout,
    children: [
      {
        path: "basicInfo",
        name: "BasicInfo",
        meta: {
          title: "基本资料"
        },
        component: () => import("@/views/user/basicInfo/index")
      },
      {
        path: "editPwd",
        name: "EditPwd",
        meta: {
          title: "修改密码"
        },
        component: () => import("@/views/user/editPwd/index")
      }
    ]
  },
  {
    path: "/test",
    component: Layout,
    children: [
      {
        path: "index",
        name: "test",
        meta: {
          title: "测试页面"
        },
        component: () => import("@/views/test/index")
      }
    ]
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  //invalid route
  // console.log(to,from)

  const {name: toName} = to;
  const {name: fromName} = from;
  if (!to.matched.length && fromName) {
    next({name: "Error404"});
    return;
  }
  const user = getSession("user");
  if (toName === "Login") {
    //jump to dashboard page if user has logged in
    if (user) next({name: "Dashboard"});
  } else {
    if (whiteList.includes(toName)) {
      next()
      return;
    }
    //not logged in
    if (!user) next({name: "Login"});
    //refresh processing
    if (fromName === null && !store.state.asyncroutes.length) {
      store.dispatch("getAsyncRoutes").then(res => {
        router.addRoutes(res);
        next();
      });
    }
  }
  next();
});
router.afterEach(() => {
  NProgress.done();
});
export default router;
